## pull state shapefiles with tigris
state_map <- states(cb = T, resolution = "5m") %>% 
  filter(STATEFP <= "56",
         )
## shift HI and AK
state_map <- shift_geometry(state_map)

state_map <- as_Spatial(state_map)

state_map <- spTransform(state_map, "+proj=longlat +datum=NAD83 +no_defs")

state_map <- fortify(state_map)

###################################
## read killings data
full_set <- readRDS("temp/geocoded_shootings_from_gva.rds") |> 
  filter(dv + home == 0,
         vics > 3)
## keep killings within 2 months of 2016, 2020 election
pre_post <- filter(full_set,
                   (date >= as.Date("2020-11-03") - months(6) &
                        date < as.Date("2020-11-03") + months(6)) |
                     (date >= as.Date("2016-11-08") - months(6) &
                        date < as.Date("2016-11-08") + months(6))) %>% 
  mutate(year = floor(year(date) / 2) * 2,
         pre = date < "2016-11-08" | (year == 2020 & date < "2020-11-03"))

pre_post <- merge(pre_post, fips_codes |> 
                    select(state_code, state_name) |> 
                    rename(state = state_name) |> 
                    distinct())

h <- st_as_sf(pre_post, coords = c("lon", "lat"),
                     crs = "+proj=longlat +datum=NAD83 +no_defs")

h <- shift_geometry(h, geoid_column = "state_code")

h <- as_Spatial(h)

h <- spTransform(h, "+proj=longlat +datum=NAD83 +no_defs")

pre_post <- bind_cols(h@data, h@coords) %>% 
  rename(latitude = coords.x2,
         longitude = coords.x1)

pre_post$pre <- ifelse(pre_post$pre,
                       "Before Election",
                       "After Election")
pre_post$pre <- factor(pre_post$pre, levels = c("Before Election", "After Election"))

##create figure 1
t <- ggplot() +
  geom_path(data = filter(state_map,
                          long > -130), mapping = aes(x = long, y = lat, group = group)) +
  geom_point(aes(x = longitude, y = latitude, fill = pre, shape = pre), color = "black",
             data = filter(pre_post, year == 2020),
             alpha = 0.5) +
  coord_map() +
  theme_bc(base_family = "LM Roman 10") +
  theme(axis.ticks = element_blank(),
        axis.text = element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank(),
        legend.position = "bottom",
        panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  scale_fill_manual(values = c("black", "red")) +
  scale_shape_manual(values = c(21, 24)) +
  labs(fill = "Mass Shooting Timing", x = NULL, y = NULL,
       shape = "Mass Shooting Timing")

t +
  ggtitle("Mass Shootings within 6 Months of 2020 Election")
saveRDS(t, "temp/map.rds")

for(i in outpaths){
  ggsave(plot = t, filename = paste0(i, "map.png"))
}

fwrite(filter(pre_post, year == 2020), "data_for_figures/figure_1.csv")

## create figure A1
t <- ggplot() +
  geom_path(data = filter(state_map,
                          long > -130), mapping = aes(x = long, y = lat, group = group)) +
  geom_point(aes(x = longitude, y = latitude, fill = pre, shape = pre), color = "black",
             data = filter(pre_post, year == 2016),
             alpha = 0.5) +
  coord_map() +
  theme_bc(base_family = "LM Roman 10") +
  theme(axis.ticks = element_blank(),
        axis.text = element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank(),
        legend.position = "bottom",
        panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  scale_fill_manual(values = c("black", "red")) +
  scale_shape_manual(values = c(21, 24)) +
  labs(fill = "Mass Shooting Timing", x = NULL, y = NULL,
       shape = "Mass Shooting Timing")

t +
  ggtitle("Mass Shootings within 6 Months of 2016 Election")
saveRDS(t, "temp/map_16.rds")

for(i in outpaths){
  ggsave(plot = t, filename = paste0(i, "map_2016.png"))
}

fwrite(filter(pre_post, year == 2016), "data_for_figures/figure_A1.csv")
